﻿using System;
using System.Windows;
using System.Windows.Controls;
using EnsureWeb.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;

namespace EnsureWeb.UI.Core
{
    public static class AttachProperties
    {
        #region SelectedToFirstAfterLoaded AttachProperty
        public static bool GetSelectedToFirstAfterLoaded(DependencyObject obj)
        {
            return (bool)obj.GetValue(SelectedToFirstAfterLoadedProperty);
        }

        public static void SetSelectedToFirstAfterLoaded(DependencyObject obj, bool value)
        {
            obj.SetValue(SelectedToFirstAfterLoadedProperty, value);
        }

        // Using a DependencyProperty as the backing store for SelectedToFirstAfterLoaded.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedToFirstAfterLoadedProperty =
            DependencyProperty.RegisterAttached("SelectedToFirstAfterLoaded", typeof(bool), typeof(AttachProperties), new PropertyMetadata(SelectedToFirstAfterLoadedChanged));

        private static void SelectedToFirstAfterLoadedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ComboBox && object.Equals(e.NewValue, true))
            {
                var cb = d as ComboBox;
                cb.Loaded += (s, ex) =>
                    {
                        (s as ComboBox).SelectedIndex = 0;
                    };
            }
        }
        #endregion
        
        #region DisableButtonSeconds AttachProperty
        public static object GetDisableButtonSeconds(DependencyObject obj)
        {
            return (object)obj.GetValue(DisableButtonSecondsProperty);
        }

        public static void SetDisableButtonSeconds(DependencyObject obj, object value)
        {
            obj.SetValue(DisableButtonSecondsProperty, value);
        }

        // Using a DependencyProperty as the backing store for DisableButtonSeconds.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DisableButtonSecondsProperty =
            DependencyProperty.RegisterAttached("DisableButtonSeconds", typeof(object), typeof(AttachProperties), new PropertyMetadata(DisableButtonSecondsChanged));

        private static void DisableButtonSecondsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int seconds = Convert.ToInt16(e.NewValue);
            var button = d as ButtonBase;
            
            button.Click += (s, se) =>
                {
                    button.IsEnabled = false;
                    var buttonContent = button.Content;
                    
                    Storyboard stbd = new Storyboard();
                    ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames();

                    int count = seconds;
                    int duration = 1;
                    for (int index = count; index >= 0; --index)
                    {
                        animation.KeyFrames.Add(new DiscreteObjectKeyFrame()
                        {
                            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, (count - index) * duration)),
                            Value = string.Format("{0}秒后才能再次使用", index.ToString())
                        });
                    }
                    Storyboard.SetTargetProperty(animation, new PropertyPath(ButtonBase.ContentProperty));
                    Storyboard.SetTarget(animation, button);
                    stbd.Children.Add(animation);
                    stbd.Begin();

                    stbd.Completed += (sbs, sbe) =>
                        {
                            button.IsEnabled = true;
                            button.Content = buttonContent;
                        };
                };
        }
        #endregion

        #region ResponsedElement AttachProperty
        public static object GetResponsedElement(DependencyObject obj)
        {
            return (object)obj.GetValue(ResponsedElementProperty);
        }

        public static void SetResponsedElement(DependencyObject obj, object value)
        {
            obj.SetValue(ResponsedElementProperty, value);
        }

        // Using a DependencyProperty as the backing store for ResponsedElement.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ResponsedElementProperty =
            DependencyProperty.RegisterAttached("ResponsedElement", typeof(object), typeof(AttachProperties), new PropertyMetadata(ResponsedElementChanged));

        private static void ResponsedElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is DataForm && d is DialogPanel)
            {
                var dp = d as DialogPanel;
                var df = e.NewValue as DataForm;

                dp.OnClose += (s, ex) =>
                    {
                        df.CancelEdit();
                    };
            }
        }
        #endregion
    }
}
